#include "parameter_config.h"
#include "main.h"
#include "lib.h"
#include "hem_port.h"
#include "flash.h"
#include "drv_can_lib.h"
#include "app_can.h"
#include "hek1008.h"
#include "hek1016.h"
#include "para.h"
#include "udp_echo.h"
#include "tcp_client.h"
#include "tcp_server.h"
#include "netconf.h"
/**
 * @brief 以太网地址掩码配置
 */
void enet_config_init(void)
{
  uc_enet_ip_addr_buf[0] = IP_ADDR0;//IP地址
  uc_enet_ip_addr_buf[1] = IP_ADDR1;
  uc_enet_ip_addr_buf[2] = IP_ADDR2;
  uc_enet_ip_addr_buf[3] = IP_ADDR3;

  uc_enet_mask_addr_buf[0] = NETMASK_ADDR0;//子网掩码
  uc_enet_mask_addr_buf[1] = NETMASK_ADDR1;
  uc_enet_mask_addr_buf[2] = NETMASK_ADDR2;
  uc_enet_mask_addr_buf[3] = NETMASK_ADDR3;

  uc_enet_gw_addr_buf[0] = GW_ADDR0;//网关
  uc_enet_gw_addr_buf[1] = GW_ADDR1;
  uc_enet_gw_addr_buf[2] = GW_ADDR2;
  uc_enet_gw_addr_buf[3] = GW_ADDR3;

  ul_tcp_server_port = LOCAL_TCP_SERVER_PORT;
  ul_tcp_client_port = LOCAL_TCP_CLIENT_PORT;
  ul_udp_port = LOCAL_UDP_PORT;
}
controller_type_t ut_controller_type;
/**
 * @brief 控制器类型初始化，可作为IO和控制器两种类型
 */
static void controller_type_init(void)
{
  ut_controller_type.io = eDisable;
  ut_controller_type.controller = eEnable;
}

static void node_config_init(hem_config_t *config) 
{
  if(ut_controller_type.controller == eEnable) 
  {
    int32_t rc = hem_config_load(&g_config);
    if (rc == 0) // succeed
    {
      if(memcmp(&g_config,config,sizeof(hem_config_t)) == 0) return;
    }

    memcpy(&g_config, config, sizeof(hem_config_t));
    g_config.config_ok = FLASH_CONFIG_OK;
    hem_config_save(&g_config);
  }

  if(ut_controller_type.io == eEnable)
  {
    int32_t rc = hem_config_load(config);
    if (rc == 0) // succeed
    {
      if((config->baudrate[0] != 125) && (config->baudrate[0] != 250) &&
        (config->baudrate[0] != 500) && (config->baudrate[0] != 1000))
      {
        config->baudrate[0] = 250;
      }
  
      if((config->baudrate[1] != 125) && (config->baudrate[1] != 250) &&
        (config->baudrate[1] != 500) && (config->baudrate[1] != 1000))
      {
        config->baudrate[1] = 250;
      }
    
      param_init(config);
      memcpy(&g_config, config, sizeof(hem_config_t));
      return;
    }
    else
    {
      param_init(&g_config);
    }
  }
}

static void pwmi_parameter_init(void)
{
  uint16_t read_buf[26]  = { 0 };
  uint16_t i = 0;
  int8_t success = 0;

  Flash_read_16bit_data(PWMI_CALIBRATION_DATA_ADDR_BASE,sizeof(read_buf)/2,read_buf);
  if((read_buf[0]  == 0x5A5A) && (read_buf[25] == 0xA5A5))
  {
    for(i = 0;i < 12;i++)
    {
      if((read_buf[i+1] < 120) || (read_buf[i+1] > 280))
      {
        success = -1;
        break;
      }
      if((read_buf[i+13] < 600) || (read_buf[i+13] > 1000))
      {
        success = -1;
        break;
      }
    }

    if(success == 0)
    {
      Lib_memcpy(uc_pwmi_param_buf_200_mA,&read_buf[1],sizeof(uc_pwmi_param_buf_200_mA)/2);
      Lib_memcpy(uc_pwmi_param_buf_800_mA,&read_buf[13],sizeof(uc_pwmi_param_buf_800_mA)/2);
    }
  }
}

uint8_t g_pwmi_store_param_sn = 0;
/**
 * @brief 存储PWMI标定参数
 */
void pwmi_parameter_store(void)
{
  uint16_t write_buf[26] = { 0 };
  uint16_t read_buf[26]  = { 0 };
  int a = 0,b = 0;

  if(g_pwmi_store_param_sn == 0) return;
  g_pwmi_store_param_sn = 0;

  Flash_read_16bit_data(PWMI_CALIBRATION_DATA_ADDR_BASE,sizeof(read_buf)/2,read_buf);
  if((read_buf[0]  == 0x5A5A) && (read_buf[25] == 0xA5A5))
  {
    a = Lib_memcmp(&read_buf[1],uc_pwmi_param_buf_200_mA,sizeof(uc_pwmi_param_buf_200_mA)/2);
    b = Lib_memcmp(&read_buf[13],uc_pwmi_param_buf_800_mA,sizeof(uc_pwmi_param_buf_800_mA)/2);
    if((a != 0) || (b != 0))
    {
      write_buf[0]  = 0x5A5A;
      write_buf[25] = 0xA5A5;
      Lib_memcpy(&write_buf[1], uc_pwmi_param_buf_200_mA,sizeof(uc_pwmi_param_buf_200_mA)/2);
      Lib_memcpy(&write_buf[13],uc_pwmi_param_buf_800_mA,sizeof(uc_pwmi_param_buf_800_mA)/2);

      Flash_erase_page((uint32_t)PWMI_CALIBRATION_DATA_ADDR_BASE);
      Flash_write_16bit_data_with_sector_erased(PWMI_CALIBRATION_DATA_ADDR_BASE,sizeof(write_buf)/2,write_buf);
    }
  }
  else
  {
    write_buf[0]  = 0x5A5A;
    write_buf[25] = 0xA5A5;
    Lib_memcpy(&write_buf[1], uc_pwmi_param_buf_200_mA,sizeof(uc_pwmi_param_buf_200_mA)/2);
    Lib_memcpy(&write_buf[13],uc_pwmi_param_buf_800_mA,sizeof(uc_pwmi_param_buf_800_mA)/2);

    Flash_erase_page((uint32_t)PWMI_CALIBRATION_DATA_ADDR_BASE);
    Flash_write_16bit_data_with_sector_erased(PWMI_CALIBRATION_DATA_ADDR_BASE,sizeof(write_buf),write_buf);
  }
}


/**
 * @brief CAN接收ID注册，只有注册了CAN ID，才会接收
 */
static void can_register(void)
{
    /*CAN0 ID 注册*/
    can0_register_std_list_id(DOWNLOAD_CMD_ID);
    can0_register_ext_list_id(HEK1008_RX_HEARTBEAT_ID_01);//心跳
    can0_register_ext_list_id(HEK1008_RX_HEARTBEAT_ID_02);
    can0_register_ext_list_id(HEK1008_RX_HEARTBEAT_ID_03);
    can0_register_ext_list_id(HEK1008_RX_HEARTBEAT_ID_04);
    can0_register_ext_list_id(HEK1008_RX_ID_01);//按键
    can0_register_ext_list_id(HEK1008_RX_ID_02);
    can0_register_ext_list_id(HEK1008_RX_ID_03);
    can0_register_ext_list_id(HEK1008_RX_ID_04);
    can0_register_ext_list_id(ID_DOWN_SET_PARA+g_config.node_id);
    can0_register_std_list_id(HEK1016_RX_ID_01);
    can0_register_std_list_id(HEK1016_RX_ID_02);
    can0_register_std_list_id(HEK1016_RX_ID_03);
    can0_register_std_list_id(HEK1016_RX_ID_04);
    /*CAN1 ID 注册*/
    can1_register_std_list_id(DOWNLOAD_CMD_ID);
    can1_register_ext_list_id(0x18FF89E0);//心跳
    can1_register_ext_list_id(0x18FF89E1);
    can1_register_ext_list_id(0x18FF89E2);
    can1_register_ext_list_id(0x18FF89E3);
    can1_register_ext_list_id(0x18FF90E0);//按键
    can1_register_ext_list_id(0x18FF90E1);
    can1_register_ext_list_id(0x18FF90E2);
    can1_register_ext_list_id(0x18FF90E3);
}
uint8_t uc_config_in1617[2] = { 0 };
/**
 * @brief 配置IN16和IN17电阻计算方法，配置此参数时，详细咨询客服
 * @param[in]  uc_config_in1617: 0: 可采样电阻，电压0~10V; 1: 可采样小电阻，电压0~3V电压
 */
void config_in16_in17(void)
{
  uc_config_in1617[0] = 1;
  uc_config_in1617[1] = 1;
}
static void port_config_init(void)
{
    uint8_t i = 0;
    // read node config para from flash
    hem_config_t default_config;

    default_config.config_ok = FLASH_CONFIG_OK;
    default_config.node_type = NODE_TYPE_HEM6801;
    default_config.node_id   = NODEID_HEM6801;
    default_config.baudrate[0] = 250;
    default_config.baudrate[1] = 250;
    default_config.can_terminal_resistor[0] = 1;
    default_config.can_terminal_resistor[1] = 1;
#if HEM_AI_NUM > 0
    for (i = 0; i < HEM_IN_PORT_NUM; i++) 
    {
        default_config.in_mode[i] = IN_MODE_5V;
    }
    if(uc_config_in1617[0] == 1) {
      default_config.in_mode[16] = IN_MODE_30K;
    } else {
      default_config.in_mode[16] = IN_MODE_5V;
    }
    if(uc_config_in1617[1] == 1) {
      default_config.in_mode[17] = IN_MODE_30K;
    } else {
      default_config.in_mode[17] = IN_MODE_5V;
    }
    default_config.ai_deadzone = 100;
    for(i = 0; i < CYCLE_MAX_NUMBER; i++)
    {
        default_config.up_cycle_max[i] = 100;
        default_config.up_cycle_min[i] = 100;
    }  
#endif
#if HEM_PWM_NUM > 0
    for(i = 0;i < HEM_PWM_FREQ_NUM;i++)
    {
        default_config.pwm_freq[i] = 200;
    }
#endif
    default_config.err_interval    = 30; // seconds
    default_config.do_timeout_ms   = 500;
    default_config.pwm_timeout_ms  = 500;
    default_config.pwmi_timeout_ms = 500;
    default_config.ao_timeout_ms   = 500;
#if HEM_PWMI_NUM > 0
    default_config.pwmi_kp100 = 112; // 112
    default_config.pwmi_ki100 = 48;	// 48
    default_config.pwmi_kd100 = 16;	// 16
#endif
    node_config_init(&default_config); 
}

/**
 * @brief 参数配置初始化
 */
void parameter_config_init(void)
{
  config_in16_in17();
  enet_config_init();
  controller_type_init();
  pwmi_parameter_init();
  can_register();
  port_config_init();
}
